package com.zyh.work4

import org.junit.Test

import scala.collection.immutable

class ListTest {

  @Test
  def work1(): Unit = {
    val list = List(3, 5, 7)
    val result: List[Int] = list.map(item => item * 2)
    println(result)
  }

  @Test
  def work2(): Unit = {
    val list = List("zhangsan", "lisi", "wangwu")
    val result: List[String] = list.map(item => item.toUpperCase())
    println(result)
  }

  @Test
  def work3(): Unit = {
    val list = List("Alice", "Bob", "Nick")
    val result: List[String] = list.filter(item => item.startsWith("A"))
    println(result)
  }

  @Test
  def work4(): Unit = {
    val list = List(1, 20, 30, 4, 5)
    val result: Int = list.reduce((total, item) => total + item)
    println(result)
  }

  @Test
  def work5(): Unit = {
    val list = List(1, 2, 3, 4, 5)
    def minus(num1:Int,num2:Int): Int = {
      num1-num2
    }

    println(list.reduceLeft(minus))
    println(list.reduceRight(minus))
    println(list.reduce(minus))
  }

  @Test
  def work6(): Unit = {
    val list = List(3, 4, 2, 7, 5)
    val result: Int = list.reduce((total, item) => if (total < item) total else item)
    println(result)
  }

  @Test
  def work7(): Unit = {
    val list = List("hello han hello", "hello han aaa aaa aaa ccc ddd uuu")
    val result: List[String] = list.flatMap(item => item.split(" "))
    //println(result)
    val map: Map[String, List[String]] = result.groupBy(item=>item)
    //println(map)
    val stringToInt: Map[String, Int] = map.map(item => item._1 -> item._2.length)
    println(stringToInt)
    val tuples: List[(String, Int)] = stringToInt.toList.sortBy(item => item._2)
    println(tuples)
  }

  @Test
  def work8(): Unit = {
    val list = List("ABC", "ZXC", "QWE")
    val result: List[String] = list.map(item => {
      item.toLowerCase()
    })
    val strings: List[String] = result.map(item => item.replace(item(0), item(0).toUpper))
    println(strings)
  }

  @Test
  def work9(): Unit = {
    val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val result: Int = list.reduce((total, item) => {
      println(total)
      total * item
    })
    println(result)
  }

  @Test
  def test(): Unit = {
    for(i <- 1 to 10 ;if i % 2 ==0; j = i+1;if j%3 == 0)println(i)

  }
}
